﻿Partial Public Class MovablePictureEventConrol


    Private m_AvailableImages As List(Of MediaFile)

    Public Sub New(ByVal p_AvailableImages As List(Of MediaFile))
        InitializeComponent()
        m_AvailableImages = p_AvailableImages
        ComboImages.ItemsSource = AvailableImagesIDs

    End Sub


    Public Property AvailableImages() As List(Of MediaFile)
        Get
            Return m_AvailableImages
        End Get
        Set(ByVal value As List(Of MediaFile))
            m_AvailableImages = value
        End Set
    End Property

    Public Property AvailableImagesIDs() As List(Of Integer)
        Get
            Dim l_IDs = (From l_Image In m_AvailableImages Select l_Image.ID).ToList
            Return l_IDs
        End Get
        Set(ByVal value As List(Of Integer))

        End Set

    End Property

    Private Sub ComboImages_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        Dim l_combo = CType(sender, ComboBox)
        Dim l_Event = CType(Me.DataContext, EventBase)
        l_Event.BackroundImageID = l_combo.SelectedItem
        Dim l_imgs As New ImageSourceConverter
        Dim l_mediaFile = (From l_file In m_AvailableImages Where l_file.ID = l_event.BackroundImageID Select l_file).Single
        Dim l_ImageSource = CType(l_imgs.ConvertFromString(l_mediaFile.Path), ImageSource)
        ImageBrush1.ImageSource = l_ImageSource


        Dim l_brush As New ImageBrush


        l_brush.ImageSource = l_ImageSource

        l_Event.GuiRectangle.Rectangel.Fill = l_brush


    End Sub

    Private Sub ButtonLocation_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            Dim l_button = CType(sender, Button)
            Dim l_Parent = getTopWindowGameEditor()

            Dim l_top = Math.Round(Canvas.GetTop(l_Parent.RubberBandProperty), 0)
            Dim l_bottom = Math.Round(l_top + l_Parent.RubberBandProperty.Height, 0)
            Dim l_left = Math.Round(Canvas.GetLeft(l_Parent.RubberBandProperty), 0)
            Dim l_right = Math.Round(l_left + l_Parent.RubberBandProperty.Width, 0)
            If l_top < 0 OrElse l_bottom < 0 OrElse l_right < 0 OrElse l_left < 0 OrElse l_top = l_bottom OrElse l_left = l_right Then
                MsgBox("Please draw the rectangle again and click here again. rectangle boundaries are out of range")
                Return
            End If


            Dim l_rec As New EventRectangle(New Point(l_left, l_top), New Point(l_right, l_bottom))
            Dim l_event = CType(Me.DataContext, EventBase)
            l_event.SecondaryLocation = l_rec

            l_button.Content = l_rec.ToString
        Catch ex As Exception

        End Try

    End Sub


#Region "Private Methods"
    Private Function getTopWindowGameEditor() As WindowGameEditor
        Dim l_Object As FrameworkElement = Me
        While l_Object IsNot Nothing
            If (TypeOf l_Object Is Window) Then
                Exit While

            End If
            If l_Object.Parent Is Nothing OrElse Not TypeOf l_Object.Parent Is FrameworkElement Then
                Exit While
            End If

            l_Object = CType(l_Object.Parent, FrameworkElement)

        End While
        If l_Object IsNot Nothing AndAlso TypeOf l_Object Is WindowGameEditor Then
            Return CType(l_Object, Window)
        Else
            Return Nothing
        End If

    End Function

#End Region

End Class
